package {
	import debug.DebugView;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	import gs.OverwriteManager;
	import gs.plugins.QuaternionsPlugin;
	import gs.plugins.TweenPlugin;
	
	import main.Scene3D;
	import main.interaction.ScreenRegions;
	import main.interaction.StateEvent;
	import main.interaction.StateManager;
	import main.util.ToolTip;
	import main.util.Util;

	
	public class Main extends Sprite
	{


		
		private var sceneFile:String = "assets/scene.dae";
		private var stateFile:String = "assets/stateDiagram.xml";
		
		private var scene:Scene3D;
		private var debugView:DebugView;
		private var stateManager:StateManager;

		private var regions:ScreenRegions;
		
		private var toolTip:ToolTip;
		
		private var sceneWidth:Number = 800;
		private var sceneHeight:Number = 450;
		
		public function Main()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			//stage.quality = StageQuality.LOW;
			
			OverwriteManager.init();
			TweenPlugin.activate([QuaternionsPlugin]); //only do this once in your SWF to activate the plugin
	
	
			stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
				if (e.keyCode == 192) {
					toggleDebugView();
				}
			});
			
			stage.addEventListener(Event.RESIZE, onStageResize);
			
			Util.loadXML(sceneFile, initScene);
		}
		
		private function initScene(xml:XML):void {
			scene = new Scene3D(xml);
			addChild(scene);
			onStageResize();
			
			regions = new ScreenRegions();
			addChild(regions);
			
			toolTip = new ToolTip();
			addChild(toolTip);
			
			scene.addEventListener(Event.COMPLETE, onInitScene);
			
			
			
		}
		
		private function onInitScene(e:Event):void {
			Util.loadXML(stateFile, initStateManager);
			onStageResize();
		}
		
		private function initStateManager(xml:XML):void {
			stateManager = new StateManager(xml, scene, regions);
			stateManager.addEventListener(StateEvent.MOUSE_OUT, onInteractiveObjectOut);
			stateManager.addEventListener(StateEvent.MOUSE_OVER, onInteractiveObjectOver);
			stateManager.addEventListener(StateEvent.MOUSE_UP, onInteractiveObjectUp);
			stateManager.addEventListener(StateEvent.REGION_OVER, onRegionOver);
			stateManager.addEventListener(StateEvent.REGION_OUT, onRegionOut);
			stateManager.addEventListener(StateEvent.REGION_UP, onRegionUp);
		}
		
		private function onInteractiveObjectOver(e:StateEvent):void {
			if (e.interactiveObject.label) {
				toolTip.setText(e.interactiveObject.label);
			}
		}

		private function onInteractiveObjectOut(e:StateEvent):void {
			toolTip.hide();
		}

		private function onInteractiveObjectUp(e:StateEvent):void {
		}

		private function onRegionOver(e:StateEvent):void {
			if (e.interactiveRegion.label) {
				toolTip.setText(e.interactiveRegion.label);
			}
		}

		private function onRegionOut(e:StateEvent):void {
			toolTip.hide();
		}

		private function onRegionUp(e:StateEvent):void {
		}
		
		private function toggleDebugView(e:Event=null):void {
			if (!debugView) {
				debugView = new DebugView();
				addChild(debugView);
			} else {
				debugView.visible = !debugView.visible;
			}
		}
		
		private function onStageResize(e:Event=null):void {
			if (scene) {
				//scene.resize(stage.stageWidth, stage.stageHeight);
				scene.x = stage.stageWidth/2 - sceneWidth/2;
				scene.y = stage.stageHeight/2 - sceneHeight/2;
				scene.resize(sceneWidth,sceneHeight);
				
				if (regions) {
					regions.resize(sceneWidth, sceneHeight);
					regions.x = scene.x;
					regions.y = scene.y;
				}
				
			}
		}
	}
}
